/**
 * Logger
 * <p/>
 * User: joakimd
 * Date: 2013-04-12
 * Time: 14:15
 */
package {
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.UncaughtErrorEvent;
import flash.text.TextField;

public final class Logger extends Sprite {

    public static const INSTANCE: Logger = new Logger(null);

    private var _history: Vector.<String>;
    private var _textField: TextField;

    public function Logger(guard: Guard) {
        _history = new Vector.<String>();
        _textField = new TextField();
        _textField.multiline = true;
        _textField.width = 640;
        _textField.height = 480;
        addChild(_textField);
    }

    public function initialize(loaderInfo: LoaderInfo): void {
        loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError);
    }

    private function onUncaughtError(event: UncaughtErrorEvent): void {
        log(event.error);
    }

    public function clear(): void {
        _history.splice(0, _history.length);
    }

    public function log(value: Object): void {
        _history.push(value.toString());
        if (_history.length > 32) {
            _history.splice(0, 1);
        }
        _textField.text = "";
        for each (var log: String in _history) {
            _textField.appendText(log + "\n");
        }
    }
}
}

class Guard {
}